<template>
  <div class="content">
    <!-- 九宫格 -->
    <div
      class=""
      style="display: flex;flex-direction: row;flex-wrap: wrap;"
    >
      <div
        v-if="idx != 5"
        class=""
        @click="itemClick(idx)"
        v-for="(obj,idx) in enterItems"
        :key="idx"
        style="width: calc(25% - 20px);padding: 10px;"
      >
        <div style="padding: 5px;flex: 1;display: flex;flex-direction: column;
					justify-content: center;align-items: center;background-color: #FFFFFF;">
          <img
            :src="obj.icon"
            style="width: 48px;height: 48px;"
            mode=""
          ></img>
          <label
            for=""
            style="font-size: 12px;margin-top: 5px;"
          >{{obj.label}}</label>
        </div>
      </div>
    </div>

    <!-- 今日跟进 -->
    <div
      class=""
      style="display: flex;padding-left: 12px;padding-right: 12px;"
    >
      <!-- left -->
      <div
        class=""
        style="flex: 1;"
      >
        <div
          class=""
          style="flex: 1;display: flex;align-items: center;padding-right: 12px;"
        >
          <div style="flex: 1;font-size: 12px;">今日跟进</div>
          <div style="font-size: 23px;font-weight: bold;color: #007AFF;">0</div>
        </div>
        <span style="font-size: 12px;color: #007AFF;">查看大学招生数据</span>
        <i
          style="font-size: 12px;color: #007AFF;"
          class="iconfont icon-arrow-right"
        ></i>
      </div>
      <!-- right -->
      <div
        class=""
        style="flex: 1;display: flex;flex-direction: column;align-items: center;"
        @click="loadSalesInfo()"
      >
        <img
          src="@/assets/home/home_loudou.png"
          style="width: 20px;height: 20px;"
          mode=""
        >
        </img>
        <div style="font-size: 13px;margin-top: 10px;">查看销售漏斗</div>
      </div>
    </div>

    <div style="height: 1px;background-color: #ebebeb;margin-top: 12px;"></div>

    <!-- record -->
    <!-- title -->
    <div style="padding: 12px;display: flex;flex-direction: row;align-items: center;justify-content: space-between;">
      <div style="font-size: 16px;font-weight: bold;">工作台消息提醒</div>
      <div style="display: flex;align-items: center;">
        <div
          class="iconfont icon-shanchu"
          style="margin-right: 5px;"
        ></div>
        <div style="font-size: 12px;">清空</div>
      </div>
    </div>

    <!-- content -->
    <div
      style="flex:1;background-color: #ffffff;height:120px;display: flex;justify-content: center;align-items: center;"
      v-if="!hasdata"
    >
      <div style="font-size: 14px;color: #999999;">暂无数据</div>
    </div>
    <div
      v-else
      style="padding-left: 12px;padding-right: 12px;display: flex;flex-direction: column;align-items: flex-start;"
      v-for="idx in 20"
    >
      <div style="font-size: 14px;text-align: left;">【学校管理】四川大学四川大学四川大学四川大学四川大学四川大学</div>
      <div style="font-size: 12px;margin-top: 5px;color: #999999;">2021年9月13日18:31:13</div>
      <div style="height: 1px;background-color: #ebebeb;margin-top: 12px;"></div>
    </div>

  </div>
</template>

<script>
import { getAuthorizeUrl, getuserinfo, Login } from "@/api/login.js";
import { getCustomerInfo } from "../../api/workbench";
import initwxsdk from "@/wxsdk.config";

export default {
  data() {
    return {
      wxuid: undefined,
      code: undefined,
      showtext: "",
      title: "Hello",
      uid: "",

      test: true,
      hasdata: false,
      enterItems: [
        {
          label: "学校",
          icon: require("@/assets/home/home01.png"),
        },
        {
          label: "教师",
          icon: require("@/assets/home/home02.png"),
        },
        {
          label: "学期",
          icon: require("@/assets/home/home03.png"),
        },
        {
          label: "课程",
          icon: require("@/assets/home/home04.png"),
        },
        {
          label: "学生",
          icon: require("@/assets/home/home05.png"),
        },
        {
          label: "商机",
          icon: require("@/assets/home/home06.png"),
        },
        {
          label: "订单",
          icon: require("@/assets/home/home07.png"),
        },
        {
          label: "渠道",
          icon: require("../../assets/home/home08.png"),
        },
      ],
    };
  },

  created() {},

  mounted() {
    let jssdkok = sessionStorage.getItem("jssdkok");
    if (jssdkok == undefined) {
      initwxsdk((status) => {
        if (status) {
          sessionStorage.setItem("jssdkok", "ok");
          this.getwxuid();
        } else {
          alert("系统错误,请检查签名");
        }
      });
    } else {
      this.netGetCustomerInfo();
    }
  },

  methods: {
    checkLogin() {
      let jsonstr = sessionStorage.getItem("loginInfo") ?? "{}";
      let loginInfo = JSON.parse(jsonstr);
      let { token, tokenKey } = loginInfo;
      if (token == undefined) {
        let hascode = this.getQueryVariable("code");
        if (hascode == false) {
          this.netGetLink();
        } else {
          this.code = hascode;
          this.netGetUID();
        }
      } else {
      }
    },

    itemClick(idx) {
      if (idx == 0) {
        this.$router.push({
          path: "/school",
        });
        return;
      }
      if (idx == 1) {
        this.$router.push({
          path: "/teacher",
        });
        return;
      }
      if (idx == 2) {
        this.$router.push({
          path: "/semester",
        });
        return;
      }
      if (idx == 3) {
        this.$router.push({
          path: "/course",
        });
        return;
      }
      if (idx == 4) {
        this.$router.push({
          path: "/student",
        });
        return;
      }
      if (idx == 5) {
        this.$router.push({
          path: "/customer",
        });
        return;
      }
      if (idx == 6) {
        this.$router.push({
          path: "/order_index",
        });
        return;
      }
      if (idx == 7) {
        this.$router.push({
          path: "/busniess",
        });
        return;
      }
    },

    loadSalesInfo() {
      this.$router.push({
        url: "/pages/sales-info/sales-info",
      });
    },

    netGetUID() {
      let self = this;
      getuserinfo({ code: this.code })
        .then((res) => {
          let { token, tokenKey } = res.data;
          if (token != null && tokenKey != null) {
            sessionStorage.setItem(
              "loginInfo",
              JSON.stringify({ token, tokenKey })
            );
          } else {
            self.$router.replace({
              path: "/500",
              query: {
                showtitle: false,
                msg: "您暂无权限使用",
              },
            });
          }
        })
        .catch((e) => {});
    },

    getwxuid() {
      let self = this;
      wx.invoke("getCurExternalContact", {}, function (res) {
        if (res.err_msg == "getCurExternalContact:ok") {
          // szw
          self.$router.push({ name: "123" });
          return;
          // end

          self.wxuid = res.userId; //返回当前外部联系人userId
          sessionStorage.setItem("wxUserId", self.wxuid);
          self.checkLogin();
        } else {
          //错误处理
          console.error("*******************");
          console.error(res);
          console.error("*******************");
        }
      });
    },

    netGetLink() {
      let fi = {
        redirect_uri: encodeURIComponent(WXWEBURI),
        state: "test",
      };
      let self = this;
      getAuthorizeUrl(fi)
        .then((res) => {
          let { msg } = res;
          window.location.href = msg;
        })
        .catch((e) => {
          self.$toast("错误" + e);
        });
    },

    netGetCustomerInfo() {
      let self = this;
      let externalUserid = sessionStorage.getItem("wxUserId");
      let fi = { externalUserid };
      getCustomerInfo(fi).then((res) => {
        let { data, code, msg } = res;
        if (code == 200) {
          let { external_contact } = data;
          sessionStorage.setItem(
            "external_contact",
            JSON.stringify(external_contact)
          );
          self.checkLogin();
        } else {
          self.$toast(msg);
        }
      });
    },
    getQueryVariable(variable) {
      var query = window.location.search.substring(1);
      var vars = query.split("&");
      for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
          return pair[1];
        }
      }

      return false;
    },
  },
};
</script>

<style>
.content {
  background-color: #ffffff;
  width: 100%;
}
</style>